﻿@{
}
@using XStudio.WebFramework.Mvc.Extensions
<div class="tt-attach">
    <ul class="op">
        @Html.WebPartButton("自动添加程序集模块", "AutoMatch", null, new { onclick = "Tree.AutoMatch(this); return false;", @class = "btn btn-insertarticle" })
        @Html.WebPartButton("清空现有模块数据", "ClearParts", null, new { onclick = "Tree.ClearParts(this); return false;", @class = "btn btn-remove", style = "color:red;" })
        @*@Html.WebPartButton("初始化模块缓存", "InitCache", null, new { onclick = "Tree.InitCache(this); return false;", @class = "btn btn-mailrefresh", title = "当前模块数据有变化时请初始化模块缓存" })*@
    </ul>
</div>
<div class="col-main">
    <div class="main-wrap box-sub" id="treePart">
        <ul id="tree">
        </ul>
    </div>
</div>
<div class="col-extra">
    <div class="box-sub hidden" id="infoPart">
        <h5 class="btn-userins">
            当前模块信息</h5>
        <div class="tip-alert-box ">
            <table class="tbl-info ">
                <tbody>
                    <tr>
                        <th>
                            唯一标识：
                        </th>
                        <td id="partID">
                        </td>
                    </tr>
                    <tr>
                        <th>
                            模块名称：
                        </th>
                        <td id="partName">
                        </td>
                    </tr>
                    <tr>
                        <th>
                            区域名称：
                        </th>
                        <td id="partAreaName">
                        </td>
                    </tr>
                    <tr>
                        <th>
                            控制器名称：
                        </th>
                        <td id="partControllerName">
                        </td>
                    </tr>
                    <tr>
                        <th>
                            动作名称：
                        </th>
                        <td id="partActionName">
                        </td>
                    </tr>
                    <tr>
                        <th>
                            标识路径：
                        </th>
                        <td id="partIDPath">
                        </td>
                    </tr>
                    <tr>
                        <th>
                            名称路径：
                        </th>
                        <td id="partNamePath">
                        </td>
                    </tr>
                    <tr>
                        <th>
                            层级：
                        </th>
                        <td id="partLevel">
                        </td>
                    </tr>
                    <tr>
                        <td colspan="2" style="text-align: center; padding: 5px;">
                            @Html.ActionLink("添加子模块", "Create", new { id = ViewBag.TypeID }, new { id = "btnInsert", onclick = "Tree.Create(this);return false;", @class = "btn-add" })
                            @Html.ActionLink("修改当前模块", "Edit", new { id = ViewBag.TypeID }, new { id = "btnEdit", onclick = "Tree.Edit(this);return false;", @class = "btn-edit" })
                            @Html.ActionLink("删除当前模块", "Delete", new { id = ViewBag.TypeID }, new { id = "btnRemove", onclick = "Tree.Delete(this);return false;", @class = "btn-del" })
                        </td>
                    </tr>
                </tbody>
            </table>
        </div>
    </div>
    <div class="box-sub">
        <h5 id="btip" class="btn-tip">
            提示</h5>
        <ul class="s001 m">
            <li>当选中的模块包含子模块时，不能删除选中模块。</li>
        </ul>
    </div>
</div>
@Html.Hidden("treeDataUrl", Url.Action("TreeData", new { id = ViewBag.TypeID }))
@Html.Hidden("moveUrl", Url.Action("Move", new { id = ViewBag.TypeID }))
@Html.Hidden("dataUrl", Url.Action("InstitutionData", new { id = ViewBag.TypeID }))
<script type="text/javascript">
    $(function () {
        $("#tree").tree({
            dnd: true,
            url: $("#treeDataUrl").val() + "?r=" + Math.random(),
            onClick: function (node) {
                //切换模块展开状态
                $(this).tree('expand', node.target);

                //呈现模块信息
                Tree.Show(node);
            },
            onDrop: function (target, source, point) {
                var target = $('#tree').tree("getNode", target);
                if (target.id == 0 && point != "append") {
                    $('#tree').tree("reload");
                    alert("模块不可与顶级节点同级！" + point);
                    return false;
                }
                Tree.Move(source, target, point);
            }
        });

        //设置树宽度及滚动条
        $("#treePart").height($(document).height() - 160 - $(".pg-footer").height() - 70 - 40);
        $("#treePart").css("overflow-y", "auto");
    });

    var Tree = {};
    Tree.Back = function (e) {
        Common.CloseTab();
    };
    Tree.Show = function (node) {
        $.getJSON($("#dataUrl").val(), { partID: node.id }, function (data) {
            $("#partName").text(data.PartName);
            $("#partID").text(data.UID);
            $("#partIDPath").text(data.IDPath);
            $("#partNamePath").text(data.NamePath);
            $("#partAreaName").text(data.AreaName);
            $("#partControllerName").text(data.ControllerName);
            $("#partActionName").text(data.ActionName);
            $("#partLevel").text(data.PartLevel);
            $("#infoPart").show();
            $("#btnRemove").show();
            if (data.UID == 0) {
                $("#btnRemove").hide();
                $("#btnEdit").hide();
            }
            else
                $("#btnEdit").show();
        });
    };
    Tree.Create = function (e) {
        var selected = $('#tree').tree("getSelected");
        if (selected == null) {
            alert("没有找到选中模块，请先选中一个模块！");
            return;
        }

        $.createWin({ url: e.href + "?partID=" + selected.id, width: 600, height: 250, title: '添加子模块' });
    };
    Tree.Edit = function (e) {
        var selected = $('#tree').tree("getSelected");
        if (selected == null) {
            alert("没有找到选中模块，请先选中需要编辑的模块！");
            return;
        }

        $.createWin({ url: e.href + "?partID=" + selected.id, width: 600, height: 350, title: '修改模块' });
    };
    Tree.Delete = function (e) {
        if (!confirm("确认要删除吗？"))
            return;

        var selected = $('#tree').tree('getSelected');
        if (selected == null) {
            alert("请选中要删除的模块。");
            return;
        }

        $.post(e.href + "?partID=" + selected.id, {}, function (data) {
            if (data.IsOK) {
                var parent = $('#tree').tree('getParent', selected.target);
                $('#tree').tree('remove', selected.target);
                $('#tree').tree('select', parent.target);
                Tree.Show(parent);
            }
            else {
                alert(data.Description);
            }
        });
    };
    Tree.Move = function (source, target, point) {
        var url = $("#moveUrl").val();
        var pointID = 0;
        if (point == "append") pointID = 1;
        else if (point == "top") pointID = 2;
        else if (point == "bottom") pointID = 3;
        var postData = { movedID: source.id, targetID: target.id, moveType: pointID };
        $.post(url, postData, function (data) {
            if (data.IsOK) {
                alert("模块移动成功。");
            }
            else {
                alert(data.Description);
                $('#tree').tree('reload');
            }
        });
    };

    Tree.AutoMatch = function (e) {
        if (!confirm("确认要运行自动添加程序集模块数据？")) return;
        $.post(e.href, {}, function (data) {
            if (data.IsOK) {
                alert("自动添加程序集模块数据成功。");
            }
            else {
                alert("自动添加程序集模块数据失败。");
            }
            $('#tree').tree('reload');
        });
    };

    Tree.ClearParts = function (e) {
        if (!confirm("确认要清空模块数据？")) return;
        $.post(e.href, {}, function (data) {
            if (data.IsOK) {
                alert("清空模块数据成功。");
            }
            else {
                alert("清空模块数据失败。");
            }
            $('#tree').tree('reload');
        });
    };

    Tree.InitCache = function (e) {
        $.post(e.href, {}, function (data) {
            if (data.IsOK) {
                alert("初始化模块缓存成功。");
            }
            else {
                alert("初始化模块缓存失败。");
            }
        });
    };
</script>
